//
//  ShellCoinCell.m
//  ShellWallert
//
//  Created by lvzhao on 2020/5/10.
//  Copyright © 2020 VV. All rights reserved.
//

#import "ShellCoinCell.h"
#import "OYCountDownManager.h"
#import "ShellCoinModel.h"


@interface ShellCoinCell ()

//期数
@property (nonatomic,strong) UILabel *periodLab;

//进度
@property (nonatomic,strong) UIView *progressRateView;
@property (nonatomic,strong) UILabel *progressNumLab;

///价格
@property (nonatomic,strong) UILabel *priceNumLab;

//待募资金(USDT)
@property (nonatomic,strong) UILabel *assetsNumLab;

//时间
@property (nonatomic,strong) UILabel *timeLab;

@property (nonatomic,strong) ShellCoinModel *coinModel;


@end

@implementation ShellCoinCell
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:kCountDownNotification object:nil];
        
        self.backgroundColor = COLOR_TINT;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    //背景
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = UIColorHex(0x123E60);
    [self.contentView addSubview:contentView];
    contentView.cornerRadius = 8;
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(K_SCREENWIDTH - 40);
        make.height.mas_equalTo(135);
    }];
    
    ///!!!:期数
    self.periodLab = [UILabel lz_labelWithText:@"" fontSize:k_textBoldFont(20) color:UIColorHex(0x4AB2E7)];
    [contentView addSubview:self.periodLab];
    [self.periodLab lz_centerAlignment];
    [self.periodLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(contentView.mas_centerX);
        make.top.mas_equalTo(10);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(28);
    }];
    
    ///!!!:进度条
    UIView *bottomProgressView = [[UIView alloc] init];
    bottomProgressView.cornerRadius = 6;
    bottomProgressView.backgroundColor = UIColorHex(0x28527D);
    [contentView addSubview:bottomProgressView];
    [bottomProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.centerX.mas_equalTo(contentView.mas_centerX);
       make.top.mas_equalTo(60);
       make.width.mas_equalTo(K_SCREENWIDTH -80);
       make.height.mas_equalTo(12);
    }];
    

    self.progressRateView = [[UIView alloc] init];
    self.progressRateView.cornerRadius = 8;
    self.progressRateView.backgroundColor = UIColorHex(0x4AB2E7);
    [contentView addSubview:self.progressRateView];
    [self.progressRateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(bottomProgressView);
        make.centerY.mas_equalTo(bottomProgressView.mas_centerY);
        make.width.mas_equalTo(K_SCREENWIDTH -80);
        make.height.mas_equalTo(16);
    }];
    
    self.progressNumLab = [UILabel lz_labelWithText:LZLocalizedString(@"即将开启") fontSize:k_textFont(10) color:UIColorHex(0x4AB2E7)];
    [self.progressRateView addSubview:self.progressNumLab];
    self.progressNumLab.backgroundColor = UIColor.whiteColor;
    [self.progressNumLab lz_centerAlignment];
    self.progressNumLab.cornerRadius = 7;
    [self.progressNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(bottomProgressView.mas_centerY);
        make.right.mas_equalTo(-1);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(14);
    }];
    
    
    
    ///!!!:价格
    CGFloat labWidth = (K_SCREENWIDTH - 80 - 20)/3.0;
    self.priceNumLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(10) color:UIColorHex(0x627D92)];
    [contentView addSubview:self.priceNumLab];
    [self.priceNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.progressRateView.mas_bottom).mas_offset(10);
        make.width.mas_equalTo(labWidth);
        make.height.mas_equalTo(50);
    }];
    
      
    ///!!!:待募资金(USDT)
    self.assetsNumLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(10) color:UIColorHex(0x627D92)];
    [contentView addSubview:self.assetsNumLab];
    [self.assetsNumLab lz_centerAlignment];
    self.assetsNumLab.adjustsFontSizeToFitWidth = YES;
    [self.assetsNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.priceNumLab.mas_right).mas_offset(10);
        make.top.mas_equalTo(self.progressRateView.mas_bottom).mas_offset(10);
        make.width.mas_equalTo(labWidth);
        make.height.mas_equalTo(self.priceNumLab);
    }];
    
    ///!!!:时间
    self.timeLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(10) color:UIColorHex(0x627D92)];
    [contentView addSubview:self.timeLab];
    [self.timeLab lz_rightAlignment];
    [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.assetsNumLab.mas_right).mas_offset(10);
        make.top.mas_equalTo(self.progressRateView.mas_bottom).mas_offset(10);
        make.width.mas_equalTo(labWidth);
        make.height.mas_equalTo(self.priceNumLab);
    }];

}

#pragma mark - 倒计时通知回调
- (void)countDownNotification {
    
    /// 计算倒计时
       NSInteger countDown = [self.coinModel.remainingSecond integerValue] - kCountDownManager.timeInterval;//self.orderModel.remainingSeconds - kCountDownManager.timeInterval;
       if (countDown < 0) {
           countDown = 0;
       };
    NSString *time = [NSString stringWithFormat:@"%02zd:%02zd:%02zd",(countDown/3600)%60, (countDown/60)%60, countDown%60];

    NSInteger status = [self.coinModel.status integerValue];
    NSString *timeStatue = @"";
       switch (status) {
           case 10:
           {
                timeStatue = LZLocalizedString(@"开启倒计时");
                NSString *timeString = [NSString stringWithFormat:@"%@\n%@",timeStatue,time];
                NSMutableAttributedString *timeAttributedString = [NSAttributedString attributedString:timeString rangeTitle:time leftFont:k_textFont(10) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
                self.timeLab.attributedText = timeAttributedString;
                  
           }
               break;
           case 20:
            {
                timeStatue = LZLocalizedString(@"距离结束");
                NSString *timeString = [NSString stringWithFormat:@"%@\n%@",timeStatue,time];
                NSMutableAttributedString *timeAttributedString = [NSAttributedString attributedString:timeString rangeTitle:time leftFont:k_textFont(10) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
                self.timeLab.attributedText = timeAttributedString;


            }
                break;
           case 30:
            {
                NSString *timeString = [NSString stringWithFormat:@"%@\n%@",LZLocalizedString(@"完成时间"),self.coinModel.completeTime];
                NSMutableAttributedString *timeAttributedString = [NSAttributedString attributedString:timeString rangeTitle:self.coinModel.completeTime leftFont:k_textFont(10) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
                self.timeLab.attributedText = timeAttributedString;
                  
            }
                break;
           default:
               
               break;
       }
}


//赋值
- (void)setModelObject:(id)modelObject{
    ShellCoinModel *coinModel = modelObject;
    self.coinModel = coinModel;
    self.periodLab.text = coinModel.periodStr;
    
    
   
    ///价格
    NSString *priceNum = [NSString stringWithFormat:@"%@",coinModel.price].saveDecimal;
    NSString *priceNumString = [NSString stringWithFormat:@"%@\n%@",LZLocalizedString(@"价格(USDT)"),priceNum];
    NSMutableAttributedString *priceNumAttributedString = [NSAttributedString attributedString:priceNumString rangeTitle:priceNum leftFont:k_textFont(10) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
    self.priceNumLab.attributedText = priceNumAttributedString;
    
    
    ///招募
    NSString *assetsNum = [NSString stringWithFormat:@"%ld/%ld",(long)coinModel.actualAmount,(long)coinModel.totalAmount];
    NSString *assetsNumString = [NSString stringWithFormat:@"%@\n%@",LZLocalizedString(@"待募资金(USDT)"),assetsNum];
    NSMutableAttributedString *assetsNumAttributedString = [NSAttributedString attributedString:assetsNumString rangeTitle:assetsNum leftFont:k_textFont(10) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
    self.assetsNumLab.attributedText = assetsNumAttributedString;
//
//    10：未开始
//    20：进行中
//    30：已完成
    
    
    CGFloat totalAmount = [[NSString stringWithFormat:@"%ld",(long)coinModel.totalAmount] floatValue];
    CGFloat progress = coinModel.actualAmount / totalAmount;
    if(progress > 1){
        progress = 1;
    }
    self.progressRateView.backgroundColor = UIColorHex(0x4AB2E7);
    CGFloat progressNumWidth = 0;
    
    CGFloat progressRateViewWidth = 0;
    NSInteger status = [coinModel.status integerValue];
    switch (status) {
        case 10:
        {
            //开始倒计时
            [self countDownNotification];
            self.progressNumLab.text = LZLocalizedString(@"即将开始");
            progressNumWidth = [self.progressNumLab.text lz_textWidthWithFontSize:self.progressNumLab.font withMaxHeight:14] + 7;
            progressRateViewWidth = progressNumWidth + 2;
        }
            break;
        case 20:
        {
            //距离结束
            [self countDownNotification];
            self.progressNumLab.text = [NSString stringWithFormat:@"%.0f%%",progress * 100];
            progressNumWidth = 40;
            
            CGFloat tempRateViewWidth = (K_SCREENWIDTH - 80) * progress;
            if(tempRateViewWidth < progressNumWidth){
                progressRateViewWidth = progressNumWidth + 2;
            } else {
                progressRateViewWidth = tempRateViewWidth;
            }
        }
            break;
        case 30:
         {
             //已完成
             NSString *timeString = [NSString stringWithFormat:@"%@\n%@",LZLocalizedString(@"完成时间"),coinModel.completeTime];
             NSMutableAttributedString *timeAttributedString = [NSAttributedString attributedString:timeString rangeTitle:coinModel.completeTime leftFont:k_textFont(10) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
            self.timeLab.attributedText = timeAttributedString;
             self.progressNumLab.text = LZLocalizedString(@"100%");
             self.progressRateView.backgroundColor = UIColorHex(0x64E3AE);
            progressNumWidth = [self.progressNumLab.text lz_textWidthWithFontSize:self.progressNumLab.font withMaxHeight:14] + 7;
             progress = 1;
             progressRateViewWidth = K_SCREENWIDTH - 80;
         }
            break;
            
        default:
            break;
    }
    
    [self.progressNumLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(progressNumWidth);
    }];
    //(K_SCREENWIDTH - 80) * progress
    [self.progressRateView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(progressRateViewWidth);
    }];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
